package com.kuiandjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kuiandjie.entity.Task;
import com.kuiandjie.util.DBHelp;
import com.kuiandjie.util.RowMapper;

public class TaskDao {
	private DBHelp<Task> db = new DBHelp<Task>();
	
	private class TaskMap implements RowMapper<Task>{

		@Override
		public Task mapperRow(ResultSet rs) throws SQLException {
			Task task = new Task();
			
			task.setId(rs.getInt("id"));
			task.setName(rs.getString("name"));
			task.setEmployeeId(rs.getInt("t_employee_id"));
			task.setDesc(rs.getString("desc"));
			task.setCreateTime(rs.getString("createtime"));
			task.setBeginTime(rs.getString("begintime"));
			task.setEndTime(rs.getString("endtime"));
			task.setGoalId(rs.getInt("t_goal_id"));
			task.setState(rs.getString("state"));
			task.setLevel(rs.getString("level"));
			task.setProgress(rs.getInt("progress"));
			
			return task;
		}
	}

	public List<Task> findTaskByGid(String curgid) {
		String sql = "SELECT id, name, t_employee_id, `desc`, createtime, begintime, endtime, t_goal_id, state,level, progress FROM t_task WHERE t_goal_id =? ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new TaskMap(), curgid);
	}

	public void insertTask(Task task) {
		String sql = "INSERT INTO t_task (name, `desc`, createtime, begintime, endtime, state, level, t_employee_id, t_goal_id, progress) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		db.executeSQL(sql, task.getName(), task.getDesc(), task.getCreateTime(), task.getBeginTime(), task.getEndTime(), task.getState(), task.getLevel(), task.getEmployeeId(), task.getGoalId(), task.getProgress());
	}

	public Task findTaskByTaskIdAndEmpId(int curTaskId, int nameId) {
		String sql = "SELECT id, name, t_employee_id, `desc`, createtime, begintime, endtime, t_goal_id, state, level, progress FROM t_task WHERE id = ? AND t_employee_id = ?";
		return db.executeQueryForObject(sql, new TaskMap(), curTaskId, nameId);
	}

	public void editByLeader(Task task) {
		String sql = "UPDATE t_task SET name = ?, t_employee_id = ?, `desc` = ?, begintime = ?, endtime = ?, state = ?, level = ? WHERE id = ?";
		db.executeSQL(sql, task.getName(), task.getEmployeeId(), task.getDesc(), task.getBeginTime(), task.getEndTime(), task.getState(), task.getLevel(), task.getId());
	}

	public void editByMember(Task task) {
		String sql = "UPDATE t_task SET state = ?, progress = ? WHERE id = ?";
		db.executeSQL(sql, task.getState(), task.getProgress(), task.getId());
	}

	public Task findById(int curTaskId) {
		String sql = "SELECT id, name, t_employee_id, `desc`, createtime, begintime, endtime, t_goal_id, state, level, progress FROM t_task WHERE id = ?";
		return db.executeQueryForObject(sql, new TaskMap(), curTaskId);
	}

	public List<Task> findTaskByEidAndPid(int cureid, String curpid) {
		String sql = "SELECT t_task.* FROM t_task LEFT JOIN t_goal ON t_task.`t_goal_id` = t_goal.`id` WHERE t_task.`t_employee_id` = ? AND t_goal.`t_project_id`=?";
		return db.executeQueryForList(sql, new TaskMap(), cureid,curpid);
	}
}
